import scrapy
from scrapy_study_05_movie.items import ScrapyStudy05MovieItem


class MvSpider(scrapy.Spider):
    name = "mv"
    allowed_domains = ["btwuji.com"]
    start_urls = ["https://btwuji.com/html/gndy/index.html"]

    def parse(self, response):
        list = "//div[@class='co_content8']//td[1]/a[2]"
        for i in response.xpath(list):
            # 获取第二页url
            url = "https://btwuji.com/" + i.xpath("./@href").extract_first()
            # 获取电影名称
            name = i.xpath("./text()").extract_first()

            # 对第二页url进行请求
            yield scrapy.Request(url=url, callback=self.parse_second, meta={"name": name})

    def parse_second(self, response):
        img_src = response.xpath("//div[@id='Zoom']//img/@src").extract_first()  # 需要注意的是span不能识别到
        # print(img_src)
        name = response.meta["name"]
        movie = ScrapyStudy05MovieItem(name=name, src=img_src)
        yield movie  # 将数据返回给管道
